1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2014 Devisualization (Richard Andrew Cattermole)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 module devisualization.util.opengl.texture;
25 import devisualization.util.opengl.function_wrappers;
26 import devisualization.image;
27 import devisualization.image.color;
28 public import devisualization.util.opengl.function_wrappers : InternalFormat, BindTextureTarget, PixelFormat;
29 import glINCOMPLETE = derelict.opengl3.gl3;
30 deprecated("de_util:opengl is going to die"):
31 
32 struct TextureImage {
33     private {
34         Image image_;
35         uint id_;
36 		uint activeTexture;
37 
38 		ubyte[] values;
39     }
40 
41     this(Image image, uint textureUnit=0) {
42         image_ = image;
43 		activeTexture = textureUnit;
44         setup();
45     }
46     
47     ~this() {
48         // destroy obj
49         glDeleteTextures(id_);
50     }
51     
52     uint opCast(T : uint)() {
53         return cast(uint)id_;
54     }
55     
56     void bind(uint unit = 0) {
57 		glActiveTexture(activeTexture + unit);
58         glBindTexture(BindTextureTarget.Texture2D, id_);
59         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.MinFilter, gl.GL_NEAREST_MIPMAP_NEAREST);
60         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.MagFilter, gl.GL_NEAREST);
61     }
62 
63     void wrapping(TextureWrapping s) {
64         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.WrapS, s);
65     }
66     
67     void wrapping(TextureWrapping s, TextureWrapping t) {
68         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.WrapS, s);
69         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.WrapT, t);
70     }
71     
72     void wrapping(TextureWrapping s, TextureWrapping t, TextureWrapping r) {
73         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.WrapS, s);
74         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.WrapT, t);
75         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.WrapR, r);
76     }
77 
78     void filters(TextureFilter min, TextureFilter mag) {
79         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.MinFilter, min);
80         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.MagFilter, mag);
81     }
82 
83     void borderColor(ref Color_RGBA color) {
84         glTexParameter(TextureParameterTarget.Texture2D, TextureParameterName.BorderColor, cast(int[])color.ubytes);
85     }
86     
87     @property {
88         Image image() {
89             return image_;
90         }
91 
92         void image(Image value) {
93             image_ = value;
94             setup();
95         }
96 
97 		uint id() {
98 			return id_;
99 		}
100     }
101 
102 	void update() {
103 		auto rgba = image.rgba;
104 		values.length = rgba.length;
105 		
106 		foreach(i, pixel; rgba.allPixels) {
107 			values[i .. i + 4] = pixel.ubytes;
108 		}
109 		
110 		glPixelStore(PixelStoreMode.UnpackingAlignment, 1);
111 		glTexImage2D(BindTextureTarget.Texture2D, 0, InternalFormat.RGBA, cast(uint)image_.width, cast(uint)image_.height, PixelFormat.RGBA, PixelDataType.UnsignedByte, values);
112 		glINCOMPLETE.glGenerateMipmap(BindTextureTarget.Texture2D);
113 		glPixelStore(PixelStoreMode.UnpackingAlignment, 4);
114 	}
115     
116     private {
117         void setup() {
118             glGenTextures(id_);
119             bind();
120             update();
121         }
122     }
123 }